package com.bistu.xiong.factory.presenter.community;

import android.support.annotation.StringRes;

import com.bistu.xiong.common.presenter.BasePresenter;
import com.bistu.xiong.factory.data.DataSource;
import com.bistu.xiong.factory.data.helper.CommunityHelper;
import com.bistu.xiong.factory.model.api.community.GetUserPostRspModel;
import net.qiujuer.genius.kit.handler.Run;
import net.qiujuer.genius.kit.handler.runable.Action;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by XiongXiaoJun on 2017/12/04.
 * LoginPresenter在原来BasePresenter进行扩充、实现LoginContract.Presenter接口
 */

public class CommunityPresenter extends BasePresenter<CommunityContract.View> implements CommunityContract.Presenter,
        DataSource.Callback<ArrayList<GetUserPostRspModel>>{

    public CommunityPresenter(CommunityContract.View view) {
        super(view);
    }

    /**
     * 刷新帖子数据
     */
    @Override
    public void freshPostData() {
        CommunityHelper.getAllUserPost(this);
    }

    /**
     * 调用API获取帖子数据  成功之后
     * @param getUserPostRspModels
     */
    @Override
    public void onDataLoaded(ArrayList<GetUserPostRspModel> getUserPostRspModels) {
        //登录成功之后
        final CommunityContract.View view = getView();
        view.stopSwipeRefresh();
        if(view!=null){
            view.freshSuccess(getUserPostRspModels);
        }
    }

    @Override
    public void onDataNotAvailable(@StringRes final int strRes) {
        final CommunityContract.View view = getView();
        view.stopSwipeRefresh();
        if (view != null){
            //强制到主线程执行
            Run.onUiAsync(new Action() {
                @Override
                public void call() {
                    //view层显示错误信息
                    view.showError(strRes);
                }
            });
        }
    }

}
